package com.example.himalaya.view.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import com.example.himalaya.R;
import com.example.himalaya.utils.LogUtil;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

public class MainIndicatorAdapter extends CommonNavigatorAdapter {

    private static final String TAG = "MainIndicatorAdapter";
    // 变量声明
    private final String[] mTitles;

    // Other UI Object
    public ColorTransitionPagerTitleView colorTransitionPageTitleView;


    // Listener
    private OnIndicatorTapClickListener mOnTabClickListener;


    public MainIndicatorAdapter(Context context) {

        // 拿到配置的string值
        mTitles = context.getResources().getStringArray(R.array.indicator_title);
    }

    @Override
    public int getCount() {
        if (mTitles != null) {
            return mTitles.length;
        }
        return 0;
    }

    @Override
    public IPagerTitleView getTitleView(Context context, final int index) {
        // 创建View
        colorTransitionPageTitleView = new ColorTransitionPagerTitleView(context);
        // 设置一般情况下的颜色为灰色
        colorTransitionPageTitleView.setNormalColor(Color.parseColor("#aaffffff"));
        // 设置选择的情况下为黑色
        colorTransitionPageTitleView.setSelectedColor(Color.parseColor("#ffffff"));
        // 设置字体大小（sp)
        colorTransitionPageTitleView.setTextSize(18);
        // 设置要显示的内容
        colorTransitionPageTitleView.setText(mTitles[index]);

        // 当点击title的时候切换内容
        colorTransitionPageTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mViewPager.setCurrentItem(index);
                // 切换viewPager的内容如果index不一样
                if (mOnTabClickListener != null) {
                    mOnTabClickListener.onTabClick(index);

                    LogUtil.d(TAG, "index --->" + index);
                }
            }
        });
        return colorTransitionPageTitleView;
    }

    @Override
    public IPagerIndicator getIndicator(Context context) {
        LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
        linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
        linePagerIndicator.setColors(Color.parseColor("#ffffff"));
        return linePagerIndicator;
    }

    public void setOnIndicatorTapClickListener(OnIndicatorTapClickListener listener) {
        this.mOnTabClickListener = listener;
    }


    public interface OnIndicatorTapClickListener {
        void onTabClick(int index);
    }
}
